<template>
  <div class="loginContainer">
    <div class="loginBox">
      <!-- 头像区域 -->
      <div class="headPortrait">
        <img src="../assets/soilDog.jpg" alt="更改头像" />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        :model="loginForm"
        status-icon
        :rules="loginFormRules"
        ref="loginFormRef"
        label-width="0"
        class="loginForm"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
            prefix-icon="el-icon-unlock"
          ></el-input>
        </el-form-item>
        <!-- 提交重置按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" @click="submitForm('loginFormRef')"
            >提交</el-button
          >
          <el-button type="info" @click="resetForm('loginFormRef')"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    let validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入用户名'));
      } else {
        if (this.loginForm.password !== '') {
          this.$refs.loginFormRef.validateField('password');
        }
        callback();
      }
    };
    let validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {
        callback();
      }
    };
    return {
      loginForm: {
        //登录表单的数据绑定对象
        username: 'admin',
        password: '123456',
      },
      loginFormRules: {
        //表单验证规则对象
        username: [
          { required: true, validator: validatePass, trigger: 'blur' },
          {
            min: 1,
            max: 6,
            message: '用户名长度在 1 到 6 位！',
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, validator: validatePass2, trigger: 'blur' },
          {
            min: 6,
            max: 12,
            message: '密码长度在 6 到 12 位！',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      //提交表单
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          //发送请求
          const { data: result } = await this.$http.post(
            'login',
            this.loginForm
          );
          //判断是否成功
          if (result.meta.status !== 200)
            return this.$message({
              message: '用户名密码or密码错误',
              type: 'error',
              center: true,
            });
          //使用消息提示框提示用户(绑定到了vue原型上)
          this.$message({ message: '登录成功', type: 'success', center: true });
          //当身份验证成功后把token值保存到本次浏览器回话结束
          window.sessionStorage.setItem('token', result.data.token);
          //路由跳转
          this.$router.push('/home');
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      //重置表单
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.loginContainer {
  height: 100%;
  background-color: #2b6b4b;
}

.loginBox {
  top: 50%;
  width: 450px;
  height: 300px;
  border-radius: 3px;
  background-color: #fff;
}

.loginBox,
.loginForm,
.headPortrait {
  position: absolute;
}

.loginBox,
.headPortrait {
  left: 50%;
  transform: translate(-50%, -50%);
}

.headPortrait,
.headPortrait img {
  width: 130px;
  height: 130px;
  border-radius: 50%;
}

.headPortrait {
  padding: 8px;
  overflow: hidden;
  border: 1px solid #eee;
  box-shadow: 0 0 10px #ddd;
}

.loginForm {
  box-sizing: border-box;
  bottom: 0;
  width: 100%;
  padding: 0 20px;
}

.btns {
  display: flex;
  justify-content: flex-end;
}
</style>
